package com.dogkr.server.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.dogkr.common.ServerAction;
import com.dogkr.server.entity.Host;
import com.dogkr.server.service.FavoritesService;
import com.dogkr.server.service.HostService;


// 设置Struts2注解
@ParentPackage(value = "login-default")
@Namespace(value = "/fvt")
@ResultPath("/WEB-INF/jsps")
// 页面文件空间
@Results({
})
public class FavoritesAction extends ServerAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	private FavoritesService favoritesService;
	@Autowired
	private HostService hostService;
	
	@Action(value = "add")
	public void add() {
		if(!favoritesService.checkHasHost(id, this.getLoginUser().getId())) {
			if(favoritesService.insert(id, this.getLoginUser().getId()) != 1) {
				res = this.getErrRes();
			}
		} else {
			res = new PageResult(-1, "已收藏过该店铺!");
		}
		outJson(res);
	}
	
	@Action(value = "del")
	public void del() {
		favoritesService.deleteFavorites(id, this.getLoginUser().getId());
		outJson(res);
	}
	
	@Action(value = "list")
	public void list() {
		List<Host> rows = hostService.selectByFavorites(this.getLoginUser().getId());
		outJson(rows);
	}
}
